package cn.yunyichina.provider.framework.exception;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.yunyichina.provider.iface.entity.cache.SessionInfo;

/**
 * 执行方法后抛出的异常
 *
 * @author zhangkewen
 */
public class CallException extends RuntimeException {

    private String resultCode;

    private String resultMessage;

    /**
     * 参数检查统一异常
     *
     * @param resultCode    错误码
     * @param resultMessage 错误信息
     */
    public CallException(String resultCode, String resultMessage) {
        this.resultCode = resultCode;
        this.resultMessage = resultMessage;
    }

    /**
     * 业务处理统一异常
     *
     * @param resultCode    错误码
     * @param resultMessage 错误信息
     * @param sessionid     sessionid
     * @param e             异常对象e
     */
    public CallException(String resultCode, String resultMessage, String sessionid, Throwable e) {

        super(resultMessage, e);
        //返回错误信息
        StackTraceElement[] elems = e.getStackTrace();

        //TODO:从缓存服务器获取
        //UserInfoCache userInfo = 从缓存服务器获取;
        SessionInfo userInfo = null;

        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        StringBuffer sb = new StringBuffer();
        sb.append(resultMessage);

        if (userInfo != null) {
            sb.append("用户[" + userInfo.getUserName() + "],");
            sb.append("UserId[" + userInfo.getUserId() + "],");
            sb.append("IP[" + userInfo.getSourceIp() + "],");
        }

        for (int i = 0; i < elems.length; i++) {

            StackTraceElement elem = elems[i];
            sb.append("在[" + date + "]时间,");
            sb.append("类[" + elem.getClassName() + "]调用[" + elem.getMethodName() + "]方法时,");
            sb.append("发生了[" + e.getClass().getName() + "]异常:");
            sb.append("异常出现在第[" + elem.getLineNumber() + "]行代码.||");
        }

        this.resultCode = resultCode;
        this.resultMessage = sb.toString();
    }

    /**
     * 业务处理统一异常
     *
     * @param resultCode    错误码
     * @param resultMessage 错误信息
     * @param e             异常对象e
     */
    public CallException(String resultCode, String resultMessage, Throwable e) {

        super(resultMessage, e);
        
        Throwable target = null;
        if (e.getCause() != null) {
            target = e.getCause();
        } else {
            target = e;
        }
      
        //返回错误信息
        StackTraceElement[] elems = target.getStackTrace();
        StringBuffer sb = new StringBuffer();
        sb.append(resultMessage);
        sb.append(" "+target.getMessage()+"  ");
        for (int i = 0; i < elems.length; i++) {
            StackTraceElement elem = elems[i];
            sb.append("$$$$$"+elem.getClassName()+" Exception:"+ e.getClass().getName()+" "+elem.getMethodName()+"("+ elem.getLineNumber()+"行)#####  ");
        }

        this.resultCode = resultCode;
        this.resultMessage = sb.toString();
    }

    public String getResultCode() {
        return resultCode;
    }

    public void setResultCode(String resultCode) {
        this.resultCode = resultCode;
    }

    public String getMessage() {
        return resultMessage;
    }

    public void setResultMessage(String resultMessage) {
        this.resultMessage = resultMessage;
    }
}
